<?php

namespace app\index\controller;

use app\index\model\Half as HalfModel;
use app\index\model\Member as MemberModel;
use app\index\service\Half as HalfService;
use app\index\service\Member as MemberService;

class Half extends Basic
{

    protected $user_id;
    protected $type_map_color = [
        0 => '#a3a3a2',
        1 => '#e60033',
        2 => '#cd5e3c',
        3 => '#c7dc68',
        4 => '#89c3eb',
    ];
    protected $type_map_color_css = [
        0 => 'activity-grey',
        1 => 'activity-red',
        2 => 'activity-yellow',
        3 => 'activity-green',
        4 => 'activity-blue',
    ];
    
//    Guilt Free Play(天蓝色):娱乐时间,比如看综艺、动漫,自己打游戏,和朋友聚餐都是这个时间;#a0d8ef 
//    Quality Work(黄色): 对自己有成长的时间,比如我这周在学科目二的驾照,在学习技术等等; #ffd900
//    Mandatory Work (土黄色): 比如听领导瞎比比自己又没办法那种,很无聊的会议;#eec362
//    Rest(绿色):吃饭的时间、睡觉的时间等等.#69b076
//    Procrastination(红色): 浪费的时间,比如自己在疯狂的刷微博、刷知乎，又特别矫情焦虑这种时间。#e2041b

    protected $type_map_name = [
        0 => '无聊',
        1 => '浪费',
        2 => '成长',
        3 => '日常',
        4 => '娱乐',
    ];

    public function __construct()
    {
        parent::__construct();
        $this->user_id = session('member_id');
        $this->assign([
            'type_map_color' => $this->type_map_color,
            'type_map_name' => $this->type_map_name,
            'type_map_color_css' => $this->type_map_color_css,
        ]);
    }



    /**
     * 列表
     * @return type
     */
    public function half_list()
    {
        $date = to_date(input('date',time()),'Y-m-d');
        $user_info = MemberModel::find($this->user_id);
        if ($this->request->isPost() && input('start_time'))
        {
            $user_info['wake_up_time'] = input('start_time', 0);
            $user_info['sleep_time'] = input('end_time', 0);
            $user_info->save();
        }
        if ($this->request->isPost() && !is_null(input('type')))
        {
            $res = HalfService::half_save($this->user_id,$date);
            return empty($this->request->isAjax()) ?$this->redirect(merge_url('half_list')) : $res;
        }
        $half_list = HalfService::getHalfList($user_info, $this->type_map_color,$date);
        $this->assign(compact([
            'user_info',
            'half_list',
            'date',
        ]));
        $list_html = $this->fetch('half_list');
        $this->assign('list_html', $list_html);
        if ($this->request->isAjax())
        {
            return $list_html;
        }
        else
        {
            return $this->fetch('half_list_base');
        }
    }

    /**
     * 统计
     * @return type
     */
    public function half_stats()
    {
        return $this->fetch('half_stats');
    }

    /**
     * 年报统计
     * @return type
     */
    public function half_year_stats()
    {
        $date = date("Y-m-d H:i:s");
        $year = to_date($date, "Y");
        $where = [
            [ 'member_id', '=', $this->user_id, ],
            [ 'type', '=', 4, ],
        ];
        $year_date_arr = current_year_date_arr($date);
        $last_year_date_arr = last_year_date_arr($date);
        $year_stats = HalfService::unitMonth($where, $year_date_arr);
        $last_year_stats = HalfService::unitMonth($where, $last_year_date_arr);
        $year_total = array_sum($year_stats);
        $last_year_total = array_sum($last_year_stats);
        $year_type_stats = HalfService::typeStats($this->user_id, $year_date_arr, $this->type_map_name);
        $this->assign(compact([
            'year_total',
            'last_year_total',
            'year_type_stats',
            'year_stats',
            'year',
        ]));
        return $this->fetch('half_year_stats');
    }

    /**
     * 月报统计
     * @return type
     */
    public function half_month_stats()
    {
        $date = date("Y-m-d H:i:s");
        $month = to_date($date, "n");
        $where = [
            [ 'member_id', '=', $this->user_id, ],
            [ 'type', '=', 4, ],
        ];
        $month_date_arr = current_month_date_arr($date);
        $last_month_date_arr = last_month_date_arr($date);
        $month_stats = HalfService::month_stats($where, $month_date_arr);
        $last_month_stats = HalfService::unitDaily($where, $last_month_date_arr);
        $month_total = array_sum($month_stats);
        $last_month_total = array_sum($last_month_stats);
        $month_type_stats = HalfService::typeStats($this->user_id, $month_date_arr, $this->type_map_name);
        $this->assign(compact([
            'month_total',
            'last_month_total',
            'month_type_stats',
            'month_stats',
            'month',
        ]));
        return $this->fetch('half_month_stats');
    }

    /**
     * 周报统计
     * @return type
     */
    public function half_week_stats()
    {
        $date = date("Y-m-d H:i:s");
        $week = to_date($date, "W");
        $where = [
            [ 'member_id', '=', $this->user_id, ],
            [ 'type', '=', 4, ],
        ];
        $week_date_arr = current_week_date_arr($date);
        $last_week_date_arr = last_week_date_arr($date);
        $week_stats = HalfService::week_stats($where, $week_date_arr);
        $last_week_stats = HalfService::unitDaily($where, $last_week_date_arr);
        $week_total = array_sum($week_stats);
        $last_week_total = array_sum($last_week_stats);
        $week_type_stats = HalfService::typeStats($this->user_id, $week_date_arr, $this->type_map_name);
        $this->assign(compact([
            'week_total',
            'last_week_total',
            'week_type_stats',
            'week_stats',
            'week',
        ]));
        return $this->fetch('half_week_stats');
    }

}
